# -*- coding: utf-8 -*-
import requests
import csv
from bs4 import BeautifulSoup

'''这是一个抓取房源的类'''
class Zhua_fang_yuan():

    def __init__(self):
        self.headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36'}
        self.lst = []
        self.bianHao = 0

    # 动态网址
    def pa_wang_ye(self, num):
        for i in range(2, num):
            url = f'https://bj.lianjia.com/zufang/pg{num}/#contentList'
            try:
                r = requests.get(url, headers=self.headers)
                # print(r)
            except:
                print(f'获取第{num}页数据失败！')
            # print(r.text)
            fy.jie_xi(r.text)

    def jie_xi(self, r):
        soup = BeautifulSoup(r, 'html.parser')
        div = soup.find_all(class_="content__list--item--main")
        # print(div, len(div))
        for xinxi in div:
            self.bianHao += 1
            print(self.bianHao)
            xx = [f'{self.bianHao}']
            lianjie = f'https://bj.lianjia.com' + xinxi.p.deng['href']
            # print(f'lianjie==={lianjie}')
            biaoti = (xinxi.p.deng).text.strip()
            # print(f'biaoti==={biaoti}')
            xx.append(biaoti)
            xx.append(lianjie)
            diqu = (xinxi.find(class_='content__list--item--des')).get_text(strip=True).split('/')
            # print(f'diqu==={diqu}')
            for i in diqu:
                xx.append(''.join(i.split()))
            jiaoTong = (xinxi.find(class_='content__list--item--bottom oneline')).get_text(strip=True)
            # print(f'jt==={jiaoTong}')
            xx.append(jiaoTong)
            weiHu = (xinxi.find(class_="content__list--item--time oneline")).get_text(strip=True)
            # print(f'wh===={weiHu}')
            xx.append(weiHu)
            zuJin = (xinxi.find(class_="content__list--item-price")).get_text(strip=True)
            # print(f'zj==={zuJin}')
            xx.append(zuJin)
            self.lst.append(xx)
        # print(self.lst)
    def xie_ru(self):
        headers = ['0', '标题', '链接', '区域', '面积', '朝向', '户型', '楼层', '其它', '更新', '租金']
        try:
            with open('租房信息.csv', 'w', encoding='utf8') as f:
                f_csv = csv.writer(f)
                f_csv.writerow(headers)
                f_csv.writerows(self.lst)
        except:
            print('写入失败')

    def run(self, num):
        self.pa_wang_ye(num)
        self.xie_ru()



fy = Zhua_fang_yuan()
if __name__ == '__main__':
    fy.run(5)
